package guoanmaker.operator.business.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.service.OperatorOrderService;
import guoanmaker.operator.business.service.OperatorRefuselistService;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
/**
 * 订单管理
 * @author zj
 *
 */
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(ApiController.OPERATOR_ORDERFORM)
public class OperatorOrderManagement {

	@Resource
	private OperatorOrderService   operatorOrderService;
	
	@Resource
	private OperatorRefuselistService  operatorRefuselistService;

	/**
	 * 
	 * @param page
	 * @param size
	 * @param status    订单状态0 全部     1待支付  2待接单  3服务中  4待确认   5退款处理中(用户发起退款)   6已完成   7已关闭  8退款完成  9退款处理中(商家已拒绝退款)
	 * @return
	 */
	@ApiOperation(value = "订单信息列表")
	@RequestMapping(value = "selectOrderInfos",method = RequestMethod.POST)
	public AjaxJson selectOrderInfos( Integer page,
            Integer size,String status){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.selectOrderInfos(page,size,status));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param page
	 * @param size
	 * @param status
	 * @param serverName
	 * @param orderTime
	 * @return
	 */
	@ApiOperation(value = "多条件查询订单信息列表")
	@RequestMapping(value = "queryOrderInfos",method = RequestMethod.POST)
	public AjaxJson queryOrderInfos( Integer page,
           Integer size,String status,String serverName,String  startOrderTime,
            String endOrderTime,String startOrderAmount,String endOrderAmount,String companyName,String orderNum,String userid,String couponid,String busid){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.queryOrderInfos(page,size,status,
					serverName,startOrderTime,endOrderTime,startOrderAmount,endOrderAmount,companyName,orderNum,userid,couponid,busid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "秒杀订单信息列表")
	@RequestMapping(value = "querymiaoOrderInfos",method = RequestMethod.POST)
	public AjaxJson querymiaoOrderInfos(String id){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.querymiaoOrderInfos(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param response
	 * @param ids           订单id
	 * @param status        订单状态0 全部     1待支付  2待接单  3服务中  4待确认   5退款处理中(用户发起退款)   6已完成   7已关闭  8退款完成  9退款处理中(商家已拒绝退款)
	 */
	@ApiOperation(value = "导出订单信息")
	@RequestMapping(value ="exportOrderInfo")
	public  void exportOrderInfo(HttpServletResponse response, String ids,String status) {
		AjaxJson ajaxJson=new AjaxJson();
		try (OutputStream out = response.getOutputStream(); Workbook wb = operatorOrderService.exportOrderInfo(ids,status);) {
			response.setContentType("application/x-msexcel");
			response.setHeader("Content-Disposition", "attachment;filename=order.xls");
			wb.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "导出订单信息")
	@RequestMapping(value ="exportAllOrderInfo")
	public  void exportAllOrderInfo(HttpServletResponse response) {
		try (OutputStream out = response.getOutputStream(); Workbook wb = operatorOrderService.exportAllOrderInfo();) {
			response.setContentType("application/x-msexcel");
			response.setHeader("Content-Disposition", "attachment;filename=order.xls");
			wb.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	@ApiOperation(value = "关闭订单原因列表")
	@RequestMapping(value ="closeOrderReasonList",method = RequestMethod.POST)
	public AjaxJson closeOrderReasonList(){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.closeOrderReasonList());
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param id
	 * @param refuseName
	 * @param content
	 * @param refunds
	 * @param userid
	 * @return
	 */
	@ApiOperation(value = "关闭订单")
	@RequestMapping(value ="closeSureOrder",method = RequestMethod.POST)
	public AjaxJson closeSureOrder(String id,String refuseName,String content,Double refunds,String userid){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.closeSureOrder(id,refuseName,content,refunds,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value="订单详情")
	@RequestMapping(value ="findbyOrderId",method = RequestMethod.POST)
	public AjaxJson findbyOrderId(String id){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.findbyOrderId(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value="优惠金额显示")
	@RequestMapping(value ="selectCouponidForinfo",method = RequestMethod.POST)
	public AjaxJson selectCouponidForinfo(String couponid){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.selectCouponidForinfo(couponid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}

	
	@ApiOperation(value = "导出秒杀订单信息列表（包含sheet页）")
	@RequestMapping(value = "exportmiaoOrderInfos")
	public  void exportmiaoOrderInfos(HttpServletResponse response,String id) {
		try (OutputStream out = response.getOutputStream(); Workbook wb = operatorOrderService.exportmiaoOrderInfos(id);) {
			response.setContentType("application/x-msexcel");
			response.setHeader("Content-Disposition", "attachment;filename=order.xls");
			wb.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
/*******************关闭原因针对订单*******************/
	
	

	
	@ApiOperation(value = "新增关闭原因（针对订单）")
	@RequestMapping(value ="addCloseOrder",method = RequestMethod.POST)
	public AjaxJson addCloseOrder(String refuseName,String userid){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.addCloseOrder(refuseName,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "编辑关闭原因（针对订单）")
	@RequestMapping(value ="editCloseOrder",method = RequestMethod.POST)
	public AjaxJson editCloseOrder(String id,String refuseName,String userid){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.editCloseOrder(id,refuseName,userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "批量删除关闭原因（针对订单）")
	@RequestMapping(value ="deleteCloseOrder",method = RequestMethod.POST)
	public AjaxJson deleteCloseOrder(String ids){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.deleteCloseOrder(ids));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
   
	@ApiOperation(value = "单个删除关闭原因（针对订单）")
	@RequestMapping(value ="deleteCloseOrderList",method = RequestMethod.POST)
	public AjaxJson deleteCloseOrderList(String id){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.deletereUnifuseList(id));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 
	 * @param page
	 * @param size
	 * @param refuseName
	 * @return
	 */
	@ApiOperation(value = "按照关闭原因查询（针对订单）")
	@RequestMapping(value ="queryCloseOrderNforList",method = RequestMethod.POST)
	public AjaxJson queryCloseOrderNforList(@RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,String refuseName){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorRefuselistService.queryCloseOrderNforList(page,size,refuseName));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	
	
	
	
	@ApiOperation(value = "雇主详情")
	@RequestMapping(value ="queryUserAndCompanyinfo",method = RequestMethod.POST)
	public AjaxJson queryUserAndCompanyinfo(String userid){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.queryUserAndCompanyinfo(userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
	}
	/**
	 * 下单推送
	 * @author lsy
	 * @param userid
	 * @param orderNumber
	 * @return
	 */
	@ApiOperation(value = "下单推送", notes = "下单推送")
	@PostMapping("/sendMessage")
	public AjaxJson sendMessage(String userid) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(operatorOrderService.sendMessage(userid));
		} catch (Exception e) {
			ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData("error");
		}
		return ajaxJson;
		
	}
}
